﻿var zongjine=0;
var userids="";
$(function(){
	 if(sessionStorage.getItem("oderids")!="" && sessionStorage.getItem("oderids")!=null){
           var code=getvl("code");
             if(code!=""){
		$.ajax({
			url: urlcore + "/api/user/getOpenid?code=" +code,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					userids= data.data.id;
					slectOrdermeliy(sessionStorage.getItem("oderids"));
					zhanghu(userids);
					$(".zhifus").show();
				} else {
					$(".zhifus").html("");
					$(".zhifus").show();
					var zhifude='<div><img style="width: 100%;" src="images/358.jpg"></div>';
					$(".zhifus").html(zhifude);
				    // alert(data.msg);
				}
			},
			error: function() {
				alert("error");
			}
		});
             }else{
            alert("请求超时");
	 	 window.history.back(-1);  
             }
	 }else{
	 	  alert("请求超时");
	 	 window.history.back(-1);  
	 }
});



	
function slectOrdermeliy(orids){
	   $("#zhifugoods").html("");
	$.ajax({
		url: urlcore + "/api/order/orderId?orderId="+orids,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
		zongjine=data.data.paymoney;
	var zhifugoods='<li>'+
'			商品数量'+
'			<h6>共'+data.data.goodsNumber+'件</h6>'+
'		</li>'+
'		<li>'+
'			商品总价'+
'			<h6>&yen;'+data.data.ordermoney+'</h6>'+
'		</li>'+
'		<li>'+
'			总运费'+
'			<h6>+&yen;'+data.data.freightmoney+'</h6>'+
'		</li>'+
'		<li class="zhifu_dingdan_da">'+
'			订单总价'+
'			<h6><em>&yen;'+data.data.paymoney+'</em></h6>'+
'		</li>';

	   $("#zhifugoods").append(zhifugoods);
	   
			}else{
				alert(data.msg);
			}
		},
		error:function() {
			alert("error");
		}
	});	

}



function zhanghu(useridss){
	  $("#zhanghuxinxi").html("&yen;"+0);
	     $("#zhanghuss").val(0);
	  if(useridss!=""){
	$.ajax({
		url: urlcore + "/api/userAccount/selectOness?userId="+useridss,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
			     $("#zhanghuxinxi").html("&yen;"+data.data.balanceMoney);
			     $("#zhanghuss").val(data.data.balanceMoney);
			}else{
				alert("异常 ");
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
}
}


function zhifu(){
	     var val=$('input:radio[name="radio1"]:checked').val();
	     if(val==null){
	     	alert("未选择支付方式");
	     	return false;
	     }else{
	     	 if(val=="01"){
	     	 	  yuezhifu();
	     	 }else if(val=="02"){
	     	 	  // alert("微信支付");
	     	 	 callpay();
	     	 }
	     }
}



//微信支付
function  zhifuweixin(){
	     if(sessionStorage.getItem("oderids")!="" && sessionStorage.getItem("oderids")!=null && userids!=""){
	       $.ajax({
					url: urlcore+"/api/pay/wxpay2?orderId="+orids+"&userId="+userids,
					type: "get",
					dataType: 'json',
					contentType: "application/json;charset=utf-8",
					success:function(data){		
					var d = data.data;
					 WeixinJSBridge.invoke(
					    'getBrandWCPayRequest',
					    {
				           "appId":d.appId,    
				           "timeStamp":d.timeStamp,        
				           "nonceStr":d.nonceStr, 
				           "package":d.package,     
				           "signType":"MD5",        
				           "paySign":d.paySign 
				       },
					    function (res){
					         if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                                if(sessionStorage.getItem("oderids")!="" && sessionStorage.getItem("oderids")!=null){
                             	chenggong(sessionStorage.getItem("oderids"));
                             	}else{
                             			 alert("请求超时");
	 	                                 window.history.back(-1);  
                             	}
					         }else{
					         	
					         	       if(sessionStorage.getItem("oderids")!="" && sessionStorage.getItem("oderids")!=null){
                             	chenggong2(sessionStorage.getItem("oderids"));
                             	}else{
                             			 alert("请求超时");
	                                 	 window.history.back(-1);  
                             	}
					         }
					    });
				},
					error:function() {
						/* Act on the event */
						alert("error");
					}
				});	
				
	   }else{
	   	 alert("请求超时");
	 	 window.history.back(-1);  
	   }
}


function chenggong2(oderid){
	$.ajax({
		url: urlcore+"/api/goodsCar/guestlist?oderid="+oderid,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
			alert("支付失败");
			window.location.href="../gotoindex.html";
			}
		},
		error: function() {
			/* Act on the event */
			//alert("error");
		}
	});
}
function chenggong(oderid){
	$.ajax({
		url: urlcore+"/api/goodsCar/guestlist?oderid="+oderid,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
			alert("支付失败");
			window.location.href="../gotoindex.html";
			}
		},
		error: function() {
			/* Act on the event */
			//alert("error");
		}
	});
}
 function callpay(){
		if (typeof WeixinJSBridge == "undefined"){
			    if (document.addEventListener){
		document.addEventListener('WeixinJSBridgeReady', zhifuweixin,false);
			    }else if (document.attachEvent){
        document.attachEvent('WeixinJSBridgeReady', zhifuweixin);
		document.attachEvent('onWeixinJSBridgeReady', zhifuweixin);
			    }
			    }else{
			        zhifuweixin();
			    }
			}
 
function gotoyuezhifu(paye){
	$.ajax({
		url: urlcore+"/api/user/yanzhengjiumima?paye="+paye,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				yuezhifu();
			}else{
				alert("支付失败");
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
	
	
	
	
}



//余额支付
function yuezhifu(){
	  if(userids!=""){
	  		$.ajax({
		url: urlcore + "/api/user/selectUserInfoByUserId?id="+userids,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				if(data.data!=null && data.data!=""){
					if(data.data.payPwd!="" || data.data.payPwd!=null){
		sessionStorage.setItem("oderidsoderids",sessionStorage.getItem("oderids"));
		sessionStorage.setItem("useridss",userids);
	     window.location.href="zhifumimatankuang.html";
					}else{
						alert("请手动到app或公众号设置支付密码");
					}
				}else{
					alert(data.msg);
				}
			}
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
	  }
}
	
	